<template>
  <div class="app">
    <!-- 头部 -->
    <van-nav-bar
      :title="title"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <!-- 中间切换区域 -->
    <router-view></router-view>
    <!-- footer -->
    <van-tabbar v-model="active">
      <van-tabbar-item icon="home-o" to="/home"></van-tabbar-item>
      <van-tabbar-item icon="friends-o" to="/friends"></van-tabbar-item>
      <van-tabbar-item icon="cart-o" to="/cart"></van-tabbar-item>
      <van-tabbar-item icon="search" to="/search"></van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
export default {
  data: () => ({
    // title: localStorage.title || 'home',
    // active: Number(localStorage.active) || 0
    title: sessionStorage.title || 'home',
    active: Number(sessionStorage.active) || 0
  }),
  methods:{
    onClickLeft(){
      // 返回上一页
      this.$router.go(-1)
    }
  },
  watch:{
    // 监听路由之监听路径
    // '$route.path'(){
    //   console.log(this.$route.meta.title)
    //   // console.log(this.title)
    //   this.title = this.$route.meta.title
    //   // 刷新之后保存到本地
    //   localStorage.title = this.$route.meta.title
    //   // 刷新之后下面图标也在对应的图片上
    //   this.active = this.$route.meta.active
    //   localStorage.active = this.$route.meta.active
    //   // 由于localStorage.active不是数字类型，因此要用Number
    //   console.log(Number(localStorage.active))
    // }

    // 监听路由之监听meta对象
    // 关闭对话本地存储的数据失效，用sessionStorage
    '$route.meta'(meta){
      console.log(meta)
      this.title = meta.title
      // 刷新之后保存到本地
      sessionStorage.title = meta.title
      // 刷新之后下面图标也在对应的图片上
      this.active = meta.active
      sessionStorage.active = meta.active
      // 由于localStorage.active不是数字类型，因此要用Number
      console.log(Number(localStorage.active))
    }
  }
}
</script>
<style lang="less" scoped>
.app{
  .title{
  color: orange;
}
}
</style>
